package com.david.mydemo.ctl_commons;

import com.david.mydemo.framework.annotation.MySysLog;
import com.david.mydemo.util.MyConstants;
import com.david.mydemo.util.MyResponse;
import com.david.mydemo.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping(value = "/user")
@Api(tags = "短信Controller")
public class SmsController {

    @PostMapping(value = "/checkSmsCaptcha")
    @ApiOperation(value = "校验短信验证码")
    @MySysLog(operate = MyConstants.OPERATE_CHECK, content = "校验短信验证码", module = MyConstants.MODULE_USER)
    public MyResponse checkSmsCaptcha(@RequestParam("smsCaptcha") String smsCaptchaCode, HttpServletRequest request) {
        Object smsCaptcha = request.getSession().getAttribute(MyConstants.SMS_CAPTCHA_KEY);
        if (smsCaptcha == null) {
            log.error("验证码超过15分钟，已经失效，请重新发送！");
            return new MyResponse().fail("验证码超过15分钟，已经失效，请重新发送！");
        }
        if (!StringUtils.equals(smsCaptcha.toString(), smsCaptchaCode)) {
            log.error("验证码不一致，请重新输入！");
            return new MyResponse().fail("验证码不一致，请重新输入！");
        }
        return new MyResponse().success();
    }

}
